package org.school.controller;

import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.school.entity.Cai;
import org.school.entity.Host;
import org.school.entity.Shop;
import org.school.service.HostService;
import org.school.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Host)表控制层
 *
 * @author makejava
 * @since 2022-12-29 00:54:25
 */
@RestController
@RequestMapping("host")
@CrossOrigin
@Log4j2
public class HostController {
    @Resource
    private HostService hostService;
    @Resource
    private ShopService shopService;
@Autowired
private StringRedisTemplate redisTemplate;
    /**
     * 分页查询
     *
     * @param host 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Host>> queryByPage(Host host, PageRequest pageRequest) {
        return ResponseEntity.ok(this.hostService.queryByPage(host, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Host> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.hostService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param host 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Host> add(Host host) {
        return ResponseEntity.ok(this.hostService.insert(host));
    }
    /**
     * 编辑数据
     *
     * @param host 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Host> edit(Host host) {
        return ResponseEntity.ok(this.hostService.update(host));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.hostService.deleteById(id));
    }
    
    @PostMapping("addShop")//添加商店
    public int insertShop(@RequestBody Shop s){
        return shopService.insert(s).getId();//返回的添加商店所属于的id
    }
    @DeleteMapping("deleteShop")//删除一个商店
    public int deleteShop(String host,int id){//删除为了安全，我们应该保证删除的是当前用户
        return shopService.deleteByHostAndId(host,id);//返回的删除所属于的id
    }
    @GetMapping("shop")//使用分页查询所有自己的商店(普通商家限制为1位）
    public ResponseEntity<PageInfo<Shop>> getShopByPage(String name, int pageNum, int size ) {

        return ResponseEntity.ok(this.hostService.findShopInfo(name,pageNum, size));
    }
    @GetMapping("info")//通过token获取Host的信息
    public ResponseEntity getInfoByToken(@RequestHeader("token") String token){
        System.out.println("获取商家信息中");
        if(token==null||!redisTemplate.hasKey(token)){
            System.out.println("获取失败");
            System.out.println(token);
            return ResponseEntity.status(404).body("error:当前token不是有效值");
        }else {
            String hostinfo=redisTemplate.opsForValue().get(token);
            System.out.println("获取成功");
            return ResponseEntity.ok().body(hostinfo);
        }
    }

}

